package com.example.app.observer;

import com.example.app.MyAsyncTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZLW on 2017/7/1.
 */

public class MySubjectImpl implements MySubject {

    private static MySubjectImpl instance;
    private List<MyObserver> myObservers;

    private MySubjectImpl(){
        myObservers = new ArrayList<>();
    }

    public static MySubjectImpl getInstance() {
        if (instance == null) {
            synchronized (MySubjectImpl.class) {
                if (instance == null) {
                    instance = new MySubjectImpl();
                }
            }
        }

        return instance;
    }


    @Override
    public void registerObserver(MyObserver observer) {
        if (!myObservers.contains(observer)) {
            myObservers.add(observer);

        }
    }

    @Override
    public void unRegisterObserver(MyObserver observer) {
        if (myObservers.contains(observer)) {
            myObservers.remove(observer);
        }
    }

    @Override
    public void notifyObserver(MyAsyncTask task) {
        for (MyObserver observer :
                myObservers) {
            observer.noticyObserver(task);
        }
    }

}
